import axios from 'axios'
import Vue from 'vue'
/**
 * 根据后端返回的文件流转为excel导出
 * @param {Object} data
 */
export function exportExcelMethod(data) {
    axios({
            method: data.method,
            url: `${data.url}${data.params ? '?' + data.params : ''}`,
            responseType: 'blob',
            headers: {
                token: localStorage.getItem('Token')
            },
            timeout: 8000,
            data: data.data
        })
        .then((res) => {
            console.log(res);
            const link = document.createElement('a')
            const blob = new Blob([res.data], {
                type: 'application/vnd.ms-excel'
            })
            link.style.display = 'none'
            link.href = URL.createObjectURL(blob)

            // link.download = res.headers['content-disposition'] //下载后文件名
            link.download = data.fileName //下载的文件名
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
            Vue.prototype.$message.success('导出成功！')
        })
        .catch((error) => {
            Vue.prototype.$message.error('参数错误！')
        })
}